//
//  ST_UINavigationController+Swizzling.swift
//  btvideo
//
//  Created by Jason on 2022/3/1.
//  Copyright © 2022 JarvanZhang. All rights reserved.
//

import UIKit


extension UINavigationController {
    
    
    
    static let nav_method_swizzling: Void = {
        
        let originalSelector1 = #selector(pushViewController(_:animated:))
        
        let swizzledSelector1 = #selector(swizzled_pushViewController(_:animated:))
        
        swizzling(UINavigationController.self, originalSelector1, swizzledSelector1)
        
        let originalSelector2 = #selector(popViewController(animated:))
        
        let swizzledSelector2 = #selector(swizzled_popViewController(animated:))
        
        swizzling(UINavigationController.self, originalSelector2, swizzledSelector2)
        
    }()
    
    @objc open func swizzled_pushViewController(_ viewController: UIViewController, animated: Bool){
        
        if let top = self.topViewController{
            
            ST_TIANRouteMonitor.default.didChanged.onNext(.finished(type: .navigationPush, value: ST_TIANPushStatus.RouteValue(form: top, to: viewController)))
            
        }
        swizzled_pushViewController(viewController, animated: animated)
        
        
    }
    
    @objc open func swizzled_popViewController(animated: Bool) -> UIViewController?{
        
        
        let top = self.topViewController
        
        let toVC = swizzled_popViewController(animated: animated)
        
        if let from = top,let to = toVC{
            
            ST_TIANRouteMonitor.default.didChanged.onNext(.finished(type: .pop, value: ST_TIANPushStatus.RouteValue(form: from, to: to)))
            
        }
        
        
        return toVC
    }

}
